<template>
  <div class="payment">
    <!-- 导航栏 -->
    <div class="navPay">
      <van-icon name="arrow-left" @click="home" />
      <span>{{radio==1?'微信支付':'支付宝支付'}}</span>
      <span></span>
    </div>
    <!-- 商品信息 -->
    <div class="ctxPay">
      <div class="titlePay">苏打优先商品</div>
      <div class="pricePay">￥{{totalMoney}}</div>
    </div>
    <!-- 收款方 -->
    <div class="context">
      <div class="collectPay">
        <div class="leftPay">收款方</div>
        <div class="rightPay">苏打优选</div>
      </div>
    </div>
    <!-- 支付按钮 -->
    <div class="btnPay">
      <van-button type="primary" size="large" @click="paying">立刻支付</van-button>
    </div>
  </div>
</template>

<script>
import { Toast } from "vant";
export default {
  data() {
    return {
      totalMoney: "",
      radio: "",
      orderId: "",
      cartId: []
    };
  },
  methods: {
    home() {
      this.$router.push(
        `/order/submit/${this.$route.params.id}/${this.radio}`
      );
    },
    Toast() {
      const toast = Toast.loading({
        duration: 0, // 持续展示 toast
        forbidClick: true,
        message: "正在支付"
      });

      let second = 3;
      const timer = setInterval(() => {
        second--;
        if (second) {
          toast.message = `支付中 ${second} 秒`;
        } else {
          clearInterval(timer);
          Toast.clear();
          this.$router.push(`/order/detail/${this.orderId}`);
        }
      }, 1000);
    },
    paying() {
      let url = "/order/pay";
      let data = {
        orderId: this.orderId,
        allFee: this.totalMoney
      };
      this.$axios
        .post(url, data)
        .then(res => {
          let url = "/cart/del";
          let data = {
            cartId: this.cartId
          };
          this.$axios
            .post(url, data)
            .then(async res => {
              this.Toast();
            })
            .catch(err => {
              console.log("err", err);
            });
        })
        .catch(err => {
          console.log("err", err);
        });
    }
  },
  created() {
    // console.log(this.$route.params);
    
    let id = this.$route.params.cartId;
    this.cartId = id.split(",");
    this.totalMoney = this.$route.params.money;
    if (this.$route.params.radio == "2") {
      this.radio = 2;
    } else {
      this.radio = 1;
    }
    this.orderId = this.$route.params.orderId;
  }
};
</script>

<style lang="less" scoped>
.navPay {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: #fff;
  height: 44px;
  font-size: 20px;
  font-weight: 500;
  // line-height: 44px;
}
.payment {
  width: 100vw;
  height: 100vh;
  background-color: #f4f4f4;
  .van-nav-bar__title {
    font-size: 20px;
  }
  .van-icon,
  .van-icon::before {
    color: black;
    font-size: 25px;
  }
  .ctxPay {
    width: 100vw;
    text-align: center;
    .titlePay {
      margin-top: 30px;
      font-size: 20px;
      font-weight: 400;
    }
    .pricePay {
      margin-top: 20px;
      color: #2d3e4f;
      font-size: 35px;
      font-weight: 600;
    }
  }
  .context {
    width: 100vw;
    border-top: 1px solid #eae8e8;
    border-bottom: 1px solid #eae8e8;
    margin-top: 20px;
    .collectPay {
      height: 50px;
      line-height: 50px;
      font-size: 14px;
      margin: 0 20px;
      display: flex;
      justify-content: space-between;
    }
  }
  .btnPay {
    width: 96%;
    margin: 0 auto;
    .van-button {
      border-radius: 10px;
      margin-top: 35px;
    }
  }
}
</style>